﻿#include "weighttask.h"
#include<QtTest/QTest>
#define MINLEN 12
weightTask::weightTask(int fd)
{
    this->fd = fd;
    IsOk=false;
    IsUp=false;
    dynamicAxis=0;
    weightValue=0;
    IsOnceWeight=false;
    weight_max=0;
}

void weightTask::runThread()
{
    char buf[32];
    char data[16];
    int type=-1;
//    float tempValue;
//    int len;
    debugOut(QString("weight task run port fd %1").arg(this->fd));
    while(1){
        //qDebug() << "weight Thread start" << endl;
        ::memset(buf,'\0',sizeof(buf));
        ::memset(data,'\0',sizeof(data));
        type=1;//systemFile::GetInstance()->weightModel.toInt();//娄烦的磅表型号是d2008  设置为3以后再修改
        switch(type){
        //金钟XK3190-A9
        case 1:
            this->getWeightValue(XK3190A9());
            break;
            //维特3190
        case 2:
            break;
            //D2008
        case 3:
            this->getWeightValue(D2008());
            break;
            //HT9800
        case 4://汾阳的磅表
            this->getWeightValue(D2008_fy());
            break;
            //维特轴计量磅XK3196B
        case 5://动态
            this->getWeightValue(DT());
            qDebug()<<"DT";
            break;
        }//end switch
        //清空接收缓存
        //qDebug() << "weight Thread end" << endl;
        usleep(50000);
    }
}
//数据接收处理
void weightTask::run()
{
    runThread();
}
bool weightTask::sendData(char *buffer)
{
    tcflush(this->fd,TCIOFLUSH);
    ::write(this->fd,buffer,strlen(buffer));
    return true;
}


float weightTask::XK3190A9()
{
    char bytes[20];
    short idx = 0;
    char data[2];
    char c;
    while(1) {
        //qint64 n = fd->read(&c, 1);
        ::memset(data,'\0',sizeof(data));
        int n=::read(fd,data,1);
        c=data[0];
        if(n <= 0) {
            usleep(50);
            continue;
        }
        if (c == 0x02 || idx > 10) {
            idx = 0; // reset
        } else if (c == 0x03 && idx == 10) { // end flag of transfer
            c = bytes[7] - '0';
            if(c >= 0 && c <= 4) {
                if(c == 0) {
                    bytes[7] = 0;
                } else {
                    int i=8;
                    bytes[i--] = 0;
                    for(; c>0; c--, i--) {
                        bytes[i] = bytes[i-1];
                    }
                    bytes[i] = '.';
                }

                float weight = atof(bytes);
                //qDebug("%f",weight);
                ::memset(bytes,'\0',sizeof(bytes));
                return weight;
            }
        }else
        { // transfering
                bytes[idx++] = c;
        }
    }
}

float weightTask::HT9800B()
{
    char bytes[5];
    short idx = 0;
    char data[2];
    char c;
    qDebug()<<"HT9800B";
    while(1) {
        ::memset(data,'\0',sizeof(data));
        int n=::read(fd,data,1);
        c=data[0];
        if(n <= 0) {
            usleep(50);
            continue;
        }
        if (c == -1 || idx > 4) { //0xFF
            if (idx == 4) {
                int a = 0;
                char buf[20];
                if (bytes[0] & 0x20)
                    buf[a++] = '-';
                char hexChar[3];
                for (int i = 3; i > 0; i--) {
                    sprintf(hexChar, "%02X", (unsigned char)bytes[i]);
                    hexChar[2] = 0;
                    //					printf("%d %s\n", i, hexChar);
                    buf[a++] = hexChar[0];
                    buf[a++] = hexChar[1];
                }
                buf[a] = 0;
                float weight = atof(buf);
                return weight;
            }
            idx = 0; // reset
        } else { // transfering
            bytes[idx++] = c;
        }
    }
}

float weightTask::XK315A()
{
    char bytes[20];
    short idx = 0;
    char data[2];
    char c;
    while(1) {
        ::memset(data,'\0',sizeof(data));
        int n=::read(fd,data,1);
        c=data[0];
        if(n <= 0) {
            usleep(50);
            continue;
        }

        if (idx > 17) {
            idx = 0; // reset
        } else if (c == -128) { // end flag of transfer, 0x80
            if (bytes[0] == 0x02 && idx == 17) {
                bytes[idx] = 0;

                int a = 0;
                char buf[20];
                if (bytes[2] & 2)
                    buf[a++] = '-';
                for (int i = 4; i < 10; i++) {
                    buf[a++] = bytes[i];
                }
                buf[a] = 0;

                float weight = atof(buf) / pow(10, bytes[1] - 34);
                return weight;
            }
            idx = 0; // reset
        } else { // transfering
            bytes[idx++] = c;
        }
    }
}

float weightTask::T8142PRO1()
{
    char bytes[20];
    short idx = 0;
    char data[2];
    char c;
    while (1) {
        ::memset(data,'\0',sizeof(data));
        int n=::read(fd,data,1);
        c=data[0];
        if (n <= 0) {
            usleep(50);
            continue;
        }
        //		SbcUtils::debug(QString("get c %1").arg((int)c));
        if (idx > 16) {
            idx = 0; // reset
            //			SbcUtils::debug("reset");
        } else if (c == -115) { //0x8D
            //			SbcUtils::debug("c == -115");
            //			SbcUtils::debug(QString("bytes[0]=%1").arg((int)bytes[0]));
            //			SbcUtils::debug(QString("idx=%1").arg(idx));
            if (bytes[0] == -126 && idx == 16) { //0x82
                bytes[idx] = 0;

                int a = 0;
                char buf[20];
                char hexChar[3];
                for (int i = 4; i < 10; i++) {
                    //					SbcUtils::debug(QString("%1 %2").arg(i).arg((int)bytes[i]));
                    //					printf("%d %02X\n", i, (unsigned char)bytes[i]);
                    sprintf(hexChar, "%02X", (unsigned char)bytes[i]);
                    hexChar[2] = 0;
                    //					printf("%d %s\n", i, hexChar);
                    buf[a++] = hexChar[1];
                    //					SbcUtils::debug(QString("%1 %2").arg(i).arg(hexChar));
                }
                buf[a] = 0;
                //SbcUtils::debug(QString("%1").arg(buf));
                float weight = atof(buf) / 1000;
                return weight;
            }
            idx = 0; // reset
            //			SbcUtils::debug("reset1");
        } else { // transfering
            bytes[idx++] = c;
        }
    }
}

float weightTask::XK3102S()
{
    char bytes[20];
    short idx = 0;
    char data[2];
    char c;
    while(1) {
        ::memset(data,'\0',sizeof(data));
        int n=::read(fd,data,1);
        c=data[0];
        if(n <= 0) {
            usleep(50);
            continue;
        }

        if (idx > 17) {
            idx = 0; // reset
        } else if (c == 0x0D) { // end flag of transfer
            if (bytes[0] == '\n' && idx == 17) {
                bytes[idx] = 0;

                int a = 0;
                char buf[20];
                for (int i = 7; i < 15; i++) {
                    if (bytes[i] != 0x20) {
                        buf[a++] = bytes[i];
                    }
                }
                buf[a] = 0;
                float weight = atof(buf) / 1000;
                return weight;
            }
            idx = 0; // reset
        } else { // transfering
            bytes[idx++] = c;
        }
    }
}

float weightTask::XK3110E()
{
    char bytes[20];
    short idx = 0;
    char data[2];
    char c;
    while(1) {
        ::memset(data,'\0',sizeof(data));
        int n=::read(fd,data,1);
        c=data[0];
        if(n <= 0) {
            usleep(50);
            continue;
        }

        if (idx > 17) {
            idx = 0; // reset
        } else if (c == 0x02) { // end flag of transfer
            if (bytes[15] == 0x0D && idx == 17) {
                bytes[idx] = 0;

                int a = 0;
                char buf[20];
                if (bytes[1] & 0x02)
                    buf[a++] = '-';

                for (int i = 3; i < 9; i++) {
                    buf[a++] = bytes[i];
                }
                buf[a] = 0;
                float weight = atof(buf) / 1000;
                return weight;
            }
            idx = 0; // reset
        } else { // transfering
            bytes[idx++] = c;
        }
    }
}

float weightTask::QDI31()
{
    char bytes[20];
    short idx = 0;
    char data[2];
    char c;
    while(1) {
        ::memset(data,'\0',sizeof(data));
        int n=::read(fd,data,1);
        c=data[0];
        if(n <= 0) {
            usleep(50);
            continue;
        }

        if (idx > 18) {
            idx = 0; // reset
        } else if (c == 0xFF) { // end flag of transfer
            if (bytes[0] == 0x02 && idx == 18) {
                bytes[idx] = 0;

                int a = 0;
                char buf[20];
                if (bytes[2] == 'B')
                    buf[a++] = '-';

                for (int i = 4; i < 10; i++) {
                    buf[a++] = bytes[i];
                }
                buf[a] = 0;
                float weight = atof(buf);
                return weight;
            }
            idx = 0; // reset
        } else { // transfering
            bytes[idx++] = c;
        }
    }
}

float weightTask::M8142PRO()
{
    char bytes[20];
    short idx = 0;
    char data[2];
    char c;
    while (1) {
        ::memset(data,'\0',sizeof(data));
        int n=::read(fd,data,1);
        c=data[0];
        if (n <= 0) {
            usleep(50);
            continue;
        }

        if (idx > 16) {
            idx = 0; // reset
        } else if (c == 0x0D) { // end flag of transfer
            if (bytes[0] == 0x02 && idx == 16) {
                bytes[idx] = 0;

                int a = 0;
                char buf[20];
                for (int i = 4; i < 10; i++) {
                    buf[a++] = bytes[i];
                }
                buf[a] = 0;
                float weight = atof(buf);
                return weight;
            }
            idx = 0; // reset
        } else if (idx != 0 || c == 0x02){ // transfering
            bytes[idx++] = c;
        }
    }
}

float weightTask::D2002E1()
{
    char bytes[20];
    short idx = 0;
    char data[2];
    char c;
    while(1) {
        ::memset(data,'\0',sizeof(data));
        int n=::read(fd,data,1);
        c=data[0];
        if(n <= 0) {
            usleep(50);
            continue;
        }

        if (idx > 17) {
            idx = 0; // reset
        } else if (c == 0x0D) { // end flag of transfer
            if(bytes[0] == '\n' && idx == 17) {
                bytes[idx] = 0;

                int a = 0;
                char buf[20];
                for (int i = 7; i < 15; i++) {
                    buf[a++] = bytes[i];
                }
                buf[a] = 0;
                float weight = atof(buf);
                return weight;
            }
            idx = 0; // reset
        } else { // transfering
            bytes[idx++] = c;
        }
    }
}

float weightTask::D2002ED()
{
    char bytes[20];
    short idx = 0;
    char data[2];
    char c;
    while (1) {
        ::memset(data,'\0',sizeof(data));
        int n=::read(fd,data,1);
        c=data[0];
        if (n <= 0) {
            usleep(50);
            continue;
        }
        if (c == 0x0D || idx > 16) { // end flag of transfer
            if (idx == 16) {
                bytes[idx] = 0;

                int a = 0;
                char buf[20];
                if (strchr(bytes, '<'))
                    buf[a++] = '-';

                for (int i = 5; i < 10; i++) {
                    buf[a++] = bytes[i];
                }
                buf[a] = 0;
                float weight = atof(buf) / 1000.0f;
                return weight;
            }
            idx = 0; // reset
        } else { // transfering
            bytes[idx++] = c;
        }
    }
}

float weightTask::T800()
{
    char bytes[20];
    short idx = 0;
    char data[2];
    char c;
    while(1) {
        ::memset(data,'\0',sizeof(data));
        int n=::read(fd,data,1);
        c=data[0];
        if(n <= 0) {
            usleep(50);
            continue;
        }

        if (c == 0x0D || idx > 16) { // end flag of transfer
            if (bytes[0] == 0x02 && idx == 16) {
                int a = 0;
                char buf[20];
                if (bytes[2] & 0x02)
                    buf[a++] = '-';

                for (int i = 4; i < 10; i++) {
                    if (bytes[i] != 0x20)
                        buf[a++] = bytes[i];
                }
                buf[a] = 0;
                float weight = atof(buf) / 1000;
                return weight;
            }
            idx = 0; // reset
        } else { // transfering
            bytes[idx++] = c;
        }
    }
}

float weightTask::XK3190D2()
{
    char bytes[20];
    short idx = 0;
    char data[2];
    char c;
    while(1) {
        ::memset(data,'\0',sizeof(data));
        int n=::read(fd,data,1);
        c=data[0];
        if(n <= 0) {
            usleep(50);
            continue;
        }

        if(c == '=' || idx > 10) {
            if (idx == 8 || idx == 7) {
                bytes[idx] = 0;
                strrev2(bytes);

                float weight = atof(bytes);
                return weight;
            }
            idx = 0; // start/end
        } else {
            bytes[idx++] = c;
        }
    }
}

float weightTask::D2002E()
{
    char bytes[20];
    short idx = 0;
    char data[2];
    char c;
    while(1) {
        ::memset(data,'\0',sizeof(data));
        int n=::read(fd,data,1);
        c=data[0];
        if(n <= 0) {
            usleep(50);
            continue;
        }

        if(c == '=' || idx > 10) {
            if (idx == 6) {
                bytes[idx] = 0;
                strrev2(bytes);


                float weight = atof(bytes) / 1000.0f;
                return weight;
            }
            idx = 0; // start/end
        } else if (c != '.') {
            bytes[idx++] = c;
        }
    }
}

float weightTask::XK3120C()
{
    char bytes[20];
    short idx = 0;
    char data[2];
    char c;
    while(1) {
        ::memset(data,'\0',sizeof(data));
        int n=::read(fd,data,1);
        c=data[0];
        if(n <= 0) {
            usleep(50);
            continue;
        }

        if(c == '=' || idx > 10) {
            if (idx == 7 || idx == 8) {
                bytes[idx] = 0;
                strrev2(bytes);

                float weight = atof(bytes);
                return weight;
            }
            idx = 0; // start/end
        } else {
            bytes[idx++] = c;
        }
    }
}

char *weightTask::strrev2(char *str)
{
    char *start = str;
    char *end = str + strlen(str) - 1;
    while(start < end) {
        char temp = *start;
        *start++ = *end;
        *end-- = temp;
    }
    return str;
}


void weightTask::getWeightValue(float weight)
{

//    if(weight > baseValue && weightValue < 5  && IsUp==true)
//    {
//        qDebug()<<"The Car is Down";
//        IsUp=false;
//        IsOnceWeight=true;
//    }
//    if(weight >= weightComp && weight > baseValue){
//        weightComp=weight;
//        IsOnceWeight=false;
//        //大于最大稳定数量，则更新取毛重
//        counter++;
//        if(counter>=maxCount){
//            IsUp=true;
//            weightValue = weight;
//            counter = 0;
//            //return weightValue;
//            //debugOut(QString("updata weight:%1").arg(weightValue));
//            //IsOk=true;
//            return;
//        }/*else
//        {
//            weightValue = 0;
//        }*/
//    }else{
//        weightValue = 0;
//        //称重值变动，则更新比较值，清零计数器
//        //weightComp = weight;
//        counter = 1;
//    }


    if(abs(weight-weightComp)<=0.030)
        weightComp=weight;
    if(weight==weightComp && weight > baseValue){

        IsOnceWeight=false;
        //大于最大稳定数量，则更新取毛重
        counter++;
        if(counter>=maxCount){
            IsUp=true;
            weightValue = weightComp;
            if(weight_max<weightValue)
                weight_max=weightValue;
            counter = 0;
            //return weightValue;
            //debugOut(QString("updata weight:%1").arg(weightValue));
            //IsOk=true;
            return;
        }

        //    weightValue=0;
    }else{
        //称重值变动，则更新比较值，清零计数器

        weightComp = weight;
        counter = 1;
        weightValue=0;
    }


}

void weightTask::getDyWeightValue(float weight)
{
    DyWeightValue=weight;
}


//山西娄烦磅表解析
float weightTask::D2008()
{
    char bytes[20];
    char weiht[7];
    short idx = 0;
    char data[2];
    char c;
    while(1) {
        //qint64 n = fd->read(&c, 1);
        ::memset(data,'\0',sizeof(data));
        int n=::read(fd,data,1);
        c=data[0];
        if(n <= 0) {
            usleep(50);
            continue;
        }
        if (c == 0x02 || idx > 10) {
            idx = 0; // reset
        } else if (c == 0x03 && idx == 10) { // end flag of transfer

            for(int i=0;i<6;i++)
                weiht[i]=bytes[i+1];
            float weight = atof(weiht);
            weight/=1000;
            ::memset(bytes,'\0',sizeof(bytes));
            ::memset(weiht,'\0',sizeof(weiht));
            return weight;
        }
        else
        { // transfering
            bytes[idx++] = c;
        }
    }
}

float weightTask::D2008_fy()
{
    char bytes[20];
    char weiht[7];
    short idx = 0;
    char data[2];
    char c;
    while(1) {
        //qint64 n = fd->read(&c, 1);
        ::memset(data,'\0',sizeof(data));
        int n=::read(fd,data,1);
        c=data[0];
        if(n <= 0) {
            usleep(50);
            continue;
        }
        if (c == 0x3D || idx >7) {
            idx = 0; // reset
        }
        else
        { // transfering
            bytes[idx++] = c;
            if(idx==7)
            {
                ::memset(weiht,'\0',sizeof(weiht));
                for(int i=0;i<7;i++)
                    weiht[i]=bytes[6-i];
                float weight = atof(weiht);
                ::memset(bytes,'\0',sizeof(bytes));

                return weight;
            }
        }
    }
}

float weightTask::DT()
{
    char bytes[30];
    char weiht[3];
    short idx = 0;
    char data[2];
    char c;
    int cmdLen=0;
    ::memset(bytes,'\0',sizeof(bytes));
    ::memset(weiht,'\0',sizeof(weiht));
    while(1) {
        //qint64 n = fd->read(&c, 1);
        ::memset(data,'\0',sizeof(data));
        int n=::read(fd,data,1);
        c=data[0];
        if(n <= 0) {
            usleep(50);
            continue;
        }
        if (c == 0xFF && idx<9) {
            idx = 0; // reset
        } else if (c == 0xFF && idx >= 9) { // end flag of transfer
            if(bytes[0]==0x01)
            {
                dynamicAxis=bytes[3];
                cmdLen=(int)bytes[1];
                float weight=0.00;
                for(int i=0;i<3;i++)
                {
                    weiht[i]=bytes[cmdLen-6+i];

                }
                weight+=(float)(((weiht[0]<<8)<<8)+(weiht[1]<<8)+weiht[2]);
                weight/=100;
                ::memset(bytes,'\0',sizeof(bytes));
                ::memset(weiht,'\0',sizeof(weiht));
                return weight;
            }else{
                idx=0;
            }
            ::memset(bytes,'\0',sizeof(bytes));
            ::memset(weiht,'\0',sizeof(weiht));
        }
        else
        { // transfering
            bytes[idx++] = c;

        }
    }
}
